from game import zz_game
from zz import zz_log, zz_img

log = zz_log.of()


class Game(zz_game.ZZ_Game):
    def __init__(self, d):
        super().__init__(d)

    def get_name(self):
        return "欢乐消消消"

    def do_task(self):
        r = self.find_img('./game/img/欢乐消消消/task03.png')
        if r.err():
            self.log_msg("未找到任务入口")
            return
        # 计算坐标
        x, y = self.d.window_size()
        x = x - int(x * 0.1)
        y = r.get_data("center")[1]
        self.log_msg(f"任务坐标：{x},{y}")
        self.d.click(x, y)
        self.do_task_view()
        self.d.sleep(2)
        self.log_msg("领取任务奖励")
        self.d.sleep(2)
        self.d.click(x, y)

    def game_alert(self):
        self.alert_close_by_file("./game/img/欢乐消消消")

    def handle_main(self):
        self.game_alert()
        # 点击进入游戏位置
        self.log_msg("等待进入游戏页面")
        if not self.img_exit("./game/img/欢乐消消消/game_main.png", 100):
            self.log_msg("进入游戏页面失败")
            return False

        self.game_alert()

        task_path = "./game/img/欢乐消消消/task04.png"
        if self.img_exit("./game/img/欢乐消消消/task.png"):
            task_path = "./game/img/欢乐消消消/task.png"

        self.img_click_if_exists('点击夜市之王', task_path, "./game/img/欢乐消消消/task01.png")
        self.log_msg("点击夜市任务")
        self.img_click("./game/img/欢乐消消消/task01.png")

        self.log_msg("进行任务列表")
        self.log_msg("准备浏览任务")
        for i in range(3):
            self.log_msg(f"开始浏览第{i + 1}次任务")
            self.do_task()
            self.d.sleep(1)

        self.log_msg("任务已完成")
        # self.d.sleep(2)
        #        self.close_game()
        return True
